package ce.com.newbluedemo.k2;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.renderscript.Float2;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import ce.com.cenewbluesdk.entity.k2.K2_SendWeatherStruct;
import ce.com.cenewbluesdk.proxy.BleFactory;
import ce.com.cenewbluesdk.proxy.CEDevK2Proxy;
import ce.com.newbluedemo.R;

/**
 * Created by YaoBinkun on 2016/12/28.
 */

public class ActivitySendWeather extends K2BaseActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener, View.OnFocusChangeListener {
    private CEDevK2Proxy k2Proxy;
    private final int TYPE_SUNNY = 0;
    private final int TYPE_CLOUDY = 1;
    private final int TYPE_RAINY = 2;
    private final int TYPE_OTHER = 3;
    private final int POSITIVE = 0;
    private final int NEGTIVE = 1;
    private int mType = 0;
    private float mTempHigh = 0, mTempLow = 0, mPM = 0;
    private int mTypeHigh = POSITIVE;
    private int mTypeLow = POSITIVE;

    private RadioGroup mRgType;
    private RadioGroup mRgHighTemp;
    private RadioGroup mRgLowTemp;


    private EditText mEtHigh;
    private EditText mEtLow;
    private EditText mEtPM;

    private boolean validHigh = true;
    private boolean validLow = true;
    private boolean validPM = true;

    private Handler mK2Handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.arg1 == 0){
                showToast("发送失败，请稍后重试！");
            } else {
                mEtHigh.setText("");
                mEtPM.setText("");
                mEtLow.setText("");
                mRgHighTemp.clearCheck();
                mRgLowTemp.clearCheck();
                mRgType.clearCheck();
                showToast("发送成功！");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_weather);
        k2Proxy = BleFactory.getInstance().getK2Proxy();
        k2Proxy.setHandler(mK2Handler);
        findViewById(R.id.btn_send_weather).setOnClickListener(this);


        mRgType = (RadioGroup) findViewById(R.id.rg_weather_type);
        mRgType.setOnCheckedChangeListener(this);
        mRgHighTemp = (RadioGroup) findViewById(R.id.rg_high_temp);
        mRgHighTemp.setOnCheckedChangeListener(this);
        mRgLowTemp = (RadioGroup) findViewById(R.id.rg_low_temp);
        mRgLowTemp.setOnCheckedChangeListener(this);

        mEtHigh = (EditText) findViewById(R.id.et_high_temp);
        mEtHigh.setOnFocusChangeListener(this);
        mEtLow = (EditText) findViewById(R.id.et_low_temp);
        mEtLow.setOnFocusChangeListener(this);
        mEtPM = (EditText) findViewById(R.id.et_pm);
        mEtPM.setOnFocusChangeListener(this);
    }

    @Override
    public void onClick(View v) {
        if (!k2Proxy.isConnectOK()) {
            Toast.makeText(this, "蓝牙已经断开", Toast.LENGTH_SHORT).show();
            return;
        }
        switch (v.getId()) {
            case R.id.btn_send_weather:
                if (mRgType.getCheckedRadioButtonId() == -1 ||
                        mRgHighTemp.getCheckedRadioButtonId() == -1 ||
                        mRgLowTemp.getCheckedRadioButtonId() == -1 ||
                        !validHigh || !validLow || !validPM) {
                    showToast("输入有误，请检查输入后重试");
                    return;
                }
                k2Proxy.getSendHelper().sendWeatherInfo(new K2_SendWeatherStruct(
                        (byte) mType, (byte) mTypeLow, (byte) mTempLow,
                        (byte) mTypeHigh, (byte) mTempHigh,
                        (byte) mPM));
                break;
            default:
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (group.getId()) {
            case R.id.rg_weather_type:
                switch (checkedId) {
                    case R.id.rb_sunny:
                        mType = TYPE_SUNNY;
                        break;
                    case R.id.rb_cloudy:
                        mType = TYPE_CLOUDY;
                        break;
                    case R.id.rb_rainy:
                        mType = TYPE_RAINY;
                        break;
                    case R.id.rb_others:
                        mType = TYPE_OTHER;
                        break;
                    default:
                        break;
                }
                break;
            case R.id.rg_high_temp:
                switch (checkedId) {
                    case R.id.rb_high_pos:
                        mTypeHigh = POSITIVE;
                        break;
                    case R.id.rb_high_neg:
                        mTypeHigh = NEGTIVE;
                        break;
                    default:
                        break;
                }
                break;
            case R.id.rg_low_temp:
                switch (checkedId) {
                    case R.id.rb_low_pos:
                        mTypeLow = POSITIVE;
                        break;
                    case R.id.rb_low_neg:
                        mTypeLow = NEGTIVE;
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
    }


    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) return;
        switch (v.getId()) {
            case R.id.et_high_temp:
                String inputHigh = mEtHigh.getText().toString();
                if (TextUtils.isEmpty(inputHigh) || !isFloat(inputHigh)) {
                    validHigh = false;
                    showToast("最高温输入不合法，请重新输入");
                } else {
                    mTempHigh = Float.parseFloat(inputHigh);
                    validHigh = true;
                }
                break;
            case R.id.et_low_temp:
                String input = mEtLow.getText().toString();
                if (TextUtils.isEmpty(input) || !isFloat(input)) {
                    validLow = false;
                    showToast("最低温输入不合法，请重新输入");
                } else {
                    mTempLow = Float.parseFloat(input);
                    validLow = true;
                }
                break;
            case R.id.et_pm:
                String inputPm = mEtPM.getText().toString();
                if (TextUtils.isEmpty(inputPm) || !isFloat(inputPm)) {
                    validPM = false;
                    showToast("PM值输入不合法，请重新输入");
                } else {
                    mPM = Float.parseFloat(inputPm);
                    validPM = true;
                }
                break;
            default:
                break;
        }
    }

    private void showToast(String input) {
        Toast.makeText(this, input, Toast.LENGTH_SHORT).show();
    }

    private boolean isFloat(String input) {
        try {
            Float.valueOf(input);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }

    }
}
